/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.activity;

import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseTopActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.UpdateInfo;
import com.simon.mengkou.data.enums.EOsType;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.dialog.AppUpdateDialog;
import com.simon.mengkou.ui.dialog.ClearCacheDialog;
import com.simon.mengkou.ui.dialog.ExitDialog;
import com.simon.mengkou.utils.UtilOuer;


import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/2/25.
 * desc    : 设置界面
 */
public class SettingActivity extends BaseTopActivity {
    //版本
    @Bind(R.id.setting_id_upgrage_version)
    TextView mTvVersion;

    //退出登录
    @Bind(R.id.setting_sign_out)
    Button mBtnExit;

    //修改密码
    @Bind(R.id.setting_modify_pwd)
    RelativeLayout mRlMoidfyPwd;

    @Override
    protected void initTop() {
        setTitle(R.string.setting_title);
        setNavigation(R.drawable.common_ic_left_nav);
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_setting);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);

        //设置当前版本
        mTvVersion.setText(getString(R.string.setting_version,
                OuerApplication.mAppInfo.getVersionName()));

        //修改密码
        mRlMoidfyPwd.setVisibility(View.GONE);
//        if(OuerApplication.mUser.getType() == EAccountType.PHONE) {
//            mRlMoidfyPwd.setVisibility(View.VISIBLE);
//        } else {
//            mRlMoidfyPwd.setVisibility(View.GONE);
//        }
    }

    /**
     * 修改密码
     */
    @OnClick(R.id.setting_modify_pwd)
    protected void modifyPwd() {
        OuerDispatcher.presentUserModifyPwdActivity(this);
    }


    /**
     * 关于我们
     */
    @OnClick(R.id.setting_about)
    protected void about() {
       OuerDispatcher.presentAboutActivity(this);
    }

    /**
     * 意见反馈
     */
    @OnClick(R.id.setting_suggestion)
    protected void goFeedback() {
        OuerDispatcher.presentFeedbackActivity(this);
    }

    /**
     * 清除缓存
     */
    @OnClick(R.id.setting_clean)
    protected void clean() {
        ClearCacheDialog dialog = new ClearCacheDialog(this);
        dialog.show();
    }

    /**
     * 账户安全
     */
    @OnClick(R.id.setting_upgrade)
    protected void upgrade() {
        AgnettyFuture future = OuerApplication.mOuerFuture.checkUpgrade(
                OuerApplication.mAppInfo.getVersionCode(),
                EOsType.ANDROID,
                OuerApplication.mAppInfo.getAppChannel(),
                new OuerFutureListener(this){

                    public void onStart(AgnettyResult result) {
                        setWaitingDialog(true);
                    };

                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        setWaitingDialog(false);
                        UpdateInfo info = (UpdateInfo)result.getAttach();

                        if(info == null || info.getOsMinVer() > OuerApplication.mAppInfo.getSdk()) {
                            UtilOuer.toast(SettingActivity.this, R.string.upgrade_update_newest);
                        } else {
                            //有新版，可更新了
                            //强制更新和可选更新的出来方式相同
                            info.setForceUpdate(false);

                            AppUpdateDialog dialog = new AppUpdateDialog(SettingActivity.this, info);
                            dialog.show();
                        }
                    }

                    @Override
                    public void onNetUnavaiable(AgnettyResult result) {
                        super.onNetUnavaiable(result);
                        setWaitingDialog(false);
                    }

                    @Override
                    public void onException(AgnettyResult result) {
                        super.onException(result);
                        setWaitingDialog(false);
                        UtilOuer.toast(SettingActivity.this, R.string.setting_other_update_failure);
                    }
                });

        attachDestroyFutures(future);
    }

    /**
     * 退出账号
     */
    @OnClick(R.id.setting_sign_out)
    protected void signOut() {
        ExitDialog exitDialog = new ExitDialog(this);
        exitDialog.show();
    }
}
